package com.jc.algorithmes.binarytree;

import java.util.Stack;

/**
 * 二叉树的前序遍历：
 * 1.先遍历根节点，再遍历左右节点
 * 2.采用迭代法实现
 */
public class PreOrderDemoV2 {

    public static void main(String[] args) {
        TreeNode node7 = new TreeNode(7,null,null);
        TreeNode node6 = new TreeNode(6,null,null);
        TreeNode node5 = new TreeNode(5,node6,node7);
        TreeNode node4 = new TreeNode(4,null,null);
        TreeNode node3 = new TreeNode(3,null,null);
        TreeNode node2 = new TreeNode(2,node4,node5);
        TreeNode node1 = new TreeNode(1,node2,node3);

        preorder(node1);
    }

    public static void preorder(TreeNode root){
        if (root != null){
            Stack<TreeNode> stack = new Stack<>();
            stack.add(root);
            while (!stack.isEmpty()){
                root = stack.pop();
                if (root != null){
                    System.out.println(root.val);
                    stack.push(root.right);
                    stack.push(root.left);
                }
            }
        }
    }

}
